﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using StripeOne.Linq.Dbml;
using StripeOne.BeautySalon.Models;

namespace StripeOne.BeautySalon.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        public ActionResult Index()
        {
            var result = new CalendarModel();
            using (var bsdc = new BeautySalonDataContext())
            {
                var userList = (from users in bsdc.Users
                                select users.Name).ToList();

                foreach (var user in userList)
                {
                    result.Users += "'" + user + "'";
                    result.Users += ",";
                }

                result.Users = result.Users.Remove(result.Users.Length - 1, 1);
            }

            return View(result);
        }

        public JsonResult GetCalendarData(string start, string end)
        {
            using (BeautySalonDataContext bsdc = new BeautySalonDataContext())
            {
                var startSplited = start.Split('/');
                var startDate = new DateTime(int.Parse(startSplited[2]), int.Parse(startSplited[1]), int.Parse(startSplited[0]));
                var endSplited = end.Split('/');
                var endDate = new DateTime(int.Parse(endSplited[2]), int.Parse(endSplited[1]), int.Parse(endSplited[0]));

                var data = from calendars in bsdc.Calendars
                           where calendars.StartDateTime >= startDate && calendars.EndDateTime <= endDate
                           select calendars;

                var calendarList = new List<CalendarJsonModel>();

                foreach (var item in data)
                {
                    var calendar = new CalendarJsonModel();
                    calendar.id = item.CalendarId;
                    calendar.start = item.StartDateTime;
                    calendar.end = item.EndDateTime;
                    calendar.title = item.Title;
                    calendar.observation = item.Observation;
                    calendar.userId = item.UserId;

                    calendarList.Add(calendar);
                }

                return Json(new { events = calendarList }, JsonRequestBehavior.AllowGet);
            }
        }
    }
}
